package com.Multithreading.Asynchronization;

import java.util.Arrays;
import static java.lang.System.*;

public class Bank {
    protected final double[] accounts;

    /**
     * @param n: The number of account.
     * @param initialBalance : Initial balance of bank reference for each account.
     * */
    public Bank(int n, double initialBalance) {
        this.accounts = new double[n];
        Arrays.fill(this.accounts, initialBalance);
    }

    /**
     * Transform the money to another account.
     * @param from: The origin account
     * @param to： The target account
     * @param money : how much
     */
    public void transform(int from, int to, double money) throws InterruptedException{
        if(this.accounts[from] < money) {
            return;
        }
        out.println(Thread.currentThread());
        this.accounts[from] -= money;
        out.println("This transfer: " + from + " to " + to + " amount: " + money);
        this.accounts[to] += money;
        out.println(this.getTotalBalance());
    }

    /**
     * Get total balance
     * @return total balance
     * */
    public double getTotalBalance() {
        double total = 0;
        for(double account : this.accounts) {
            total += account;
        }
        return total;
    }

    /**
     * @return : The amount of account
     * */
    public int getSize() {
        return this.accounts.length;
    }

}
